package freemind.controller;

import java.util.HashMap;
import netscape.javascript.JSObject;
import freemind.controller.ChangeTextPanel;
import freemind.controller.OuvinteControllerAction;
import freemind.main.FreeMindApplet;
import freemind.modes.MindMap;
import freemind.modes.browsemode.BrowseMapModel;
import freemind.modes.browsemode.BrowseNodeModel;
import freemind.view.mindmapview.NodeView;

public class CustomControllerBase implements CustomController {
	protected FreeMindApplet frame;
	protected BrowseMapModel model;
	protected JSObject windowBrowser;
	protected int eneg_modulo;

	protected int virtualId;

	protected HashMap<Integer, BrowseNodeModel> criados;
	protected NodeView linkNode1;
	protected NodeView copiedNode;
	protected OuvinteControllerAction ouvinteAction;

	protected NodeView nodeSelectToDelete;

	public static CustomController getCustomControllerFromModulo(FreeMindApplet frame){
		String eneg_modulo_str = frame.getParameter("eneg_modulo");
		
		if( eneg_modulo_str.equalsIgnoreCase("Riscos") ){
			return new CustomControllerRiscos(frame);
		}
		else if ( eneg_modulo_str.equalsIgnoreCase("Dialogo") ){
			return new CustomControllerDialogo(frame);
		}
		
		return new CustomControllerBase( frame );
	}
	
	public CustomControllerBase(FreeMindApplet frame) {
		this.frame = frame;
		this.model = (BrowseMapModel) frame.getController().getModel();
		this.ouvinteAction = new OuvinteControllerAction(this);
		
		criados = new HashMap<Integer, BrowseNodeModel>();
		virtualId = 0;

		windowBrowser = (JSObject) frame.getWindowBrowser();
		// responseLoad(
		// "<mindmap ENEG_MODULO='1' PROTOCOLO='2' OPERACAO='2'><meta><nome/><autor/><data_criacao/><data_atualizacao/></meta><arvore><no><meta><virtual_id>0</virtual_id><id>267</id><id_pai>0</id_pai><id_raiz>0</id_raiz><is_left>true</is_left><tipo>1</tipo><text>Modifiquei \0/</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos><no><meta><virtual_id>0</virtual_id><id>268</id><id_pai>267</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>2</tipo><text>Qual e</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos><no><meta><virtual_id>0</virtual_id><id>269</id><id_pai>268</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>2</tipo><text>A desse programa</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos><no><meta><virtual_id>0</virtual_id><id>296</id><id_pai>269</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>1</tipo><text>Novo no</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos/></no></filhos></no><no><meta><virtual_id>0</virtual_id><id>270</id><id_pai>268</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>2</tipo><text>Mudei e vai dar certo</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos/></no></filhos></no><no><meta><virtual_id>0</virtual_id><id>350</id><id_pai>267</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>1</tipo><text>Novo no</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos><no><meta><virtual_id>0</virtual_id><id>351</id><id_pai>350</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>1</tipo><text>Novo no</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos/></no></filhos></no><no><meta><virtual_id>0</virtual_id><id>352</id><id_pai>267</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>1</tipo><text>POISON</text><link>296</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos><no><meta><virtual_id>0</virtual_id><id>353</id><id_pai>352</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>1</tipo><text>Novo no</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/><filhos/></no></filhos></no></filhos></no></arvore></mindmap>");
		// responseSave("<mindmap ENEG_MODULO='1' PROTOCOLO='1' OPERACAO='2'><meta><nome/><autor/><data_criacao/><data_atualizacao/></meta><criados><no><meta><virtual_id>0</virtual_id><id>318</id><id_pai>267</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>1</tipo><text>Novo no</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/></no><no><meta><virtual_id>0</virtual_id><id>319</id><id_pai>267</id_pai><id_raiz>267</id_raiz><is_left>true</is_left><tipo>1</tipo><text>Novo no</text><link>0</link><risco_id>0</risco_id><dialogo_id>0</dialogo_id></meta><extras/></no></criados></mindmap>");
	}

	public int getEnegModulo() {
		return this.eneg_modulo;
	}

	public BrowseNodeModel getNodeByIndex(Object parent, int index) {
		return (BrowseNodeModel) this.model.getChild(parent, index);
	}

	protected BrowseNodeModel searchNodeById(Object parent, int id) {

		BrowseNodeModel p = (BrowseNodeModel) parent;

		if (p.getId() == id) {
			return p;
		}

		for (int i = 0; i < model.getChildCount(parent); i++) {
			p = searchNodeById(model.getChild(parent, i), id);
			if (p != null) {
				return p;
			}

		}

		return null;
	}

	public void cancelOperation(ChangeTextPanel panel) {
		// TODO Auto-generated method stub

	}

	public void changeNode(ChangeTextPanel panel, MindMap model) {
		// TODO Auto-generated method stub

	}

	public void changeType(NodeView node) {
		// TODO Auto-generated method stub

	}

	public void editNode(ChangeTextPanel panel) {
		// TODO Auto-generated method stub

	}

	public void insertNode(NodeView node, boolean root) {
		// TODO Auto-generated method stub

	}

	public void newMap(String nome, String autor) {
		this.frame.getView().moveToRoot();
	}

	public void removerLink(NodeView node) {
		// TODO Auto-generated method stub

	}

	public void requestDelete(NodeView node) {
		// TODO Auto-generated method stub

	}

	public String requestLoad(int rootId) {
		// TODO Auto-generated method stub
		return null;
	}

	public String requestSave() {
		// TODO Auto-generated method stub
		return null;
	}

	public void responseDelete(boolean toDelete) {
		// TODO Auto-generated method stub

	}

	public String responseLoad(String protocol) {
		// TODO Auto-generated method stub
		return "";
	}

	public String responseSave(String protocol) {
		// TODO Auto-generated method stub
		return "";
	}

	public void tooglePosition(NodeView node) {
		// TODO Auto-generated method stub

	}

	public void willLinkNode(NodeView node) {
		// TODO Auto-generated method stub

	}

	public void goToTheLink(int link) {
		// TODO Auto-generated method stub
		
	}

	public int getNodeVisibleFunctions(NodeView node) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void copiaNode(NodeView node) {
		// TODO Auto-generated method stub
		
	}

	public void pasteNode(NodeView selectedNode) {
		// TODO Auto-generated method stub
		
	}
	
	public void appletGetStarted(){
		windowBrowser.eval("iniciar(" + this.eneg_modulo + ");");		
	}

	public void appletHasLoadedContent() {
		windowBrowser.eval("appletCarregouConteudo();");			
	}

	public void testaEnvioByte() {
		// TODO Auto-generated method stub
		
	}

}
